'use strict';

import {ajax,nameSpace } from 'utils/index';

let ns = nameSpace('Mjs1');

export const SET_FIELD = ns('SET_FIELD');
export const SET_ZHEKOU_TYPE = ns('SET_ZHEKOU_TYPE');
export const CREAT_ZHEKOU_START = ns('CREAT_ZHEKOU_START');
export const CREAT_ZHEKOU_SET_ITEMS = ns('CREAT_ZHEKOU_SET_ITEMS');
export const CREAT_ZHEKOU_NEED_ITEMS = ns('CREAT_ZHEKOU_NEED_ITEMS');
export const CREAT_ZHEKOU_DONE = ns('CREAT_ZHEKOU_DONE');
export const CREAT_ZHEKOU_ERR = ns('CREAT_ZHEKOU_ERR');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const OPEN_AREA_DIALOG = ns('OPEN_AREA_DIALOG');
export const SET_AREA = ns('SET_AREA');
export const SET_GIFT = ns('SET_GIFT');
export const REMOVE_GIFT = ns('REMOVE_GIFT');
export const CREATE_NEW = ns('CREATE_NEW');

export function setField(name,value) {
  return {type: SET_FIELD,name: name,value:value};
}
export function closeDialog() {
  return {type: CLOSE_DIALOG,visible:false};
}
export function setArea(exclude_area) {
	  return {type: SET_AREA,exclude_area};
	}
export function createNew() {
	  return {type: CREATE_NEW};
	}
export function addItemsToActive(joinItems,activityId){

		return (dispatch) => {
			if(!joinItems || joinItems.length<1){
				 dispatch({
						type: CREAT_ZHEKOU_ERR,
						message:"没有要添加的商品！",
						loginFalse:false
						});
				 return;
			}
			if(!activityId){
				 dispatch({
						type: CREAT_ZHEKOU_ERR,
						message:"没有活动信息，请重新创建！",
						loginFalse:false
						});
				 return;
			}
			const ids = [];
			joinItems.map(item=>{
				ids.push(item.num_iid);
			});
			const params = {ids:ids.join(","),activity_id:activityId}
		    //发送请求前
			if(window.workbench){
				//千牛环境执行创建折扣,含有workbench参数为千牛环境
				  window.QN.top.invoke( {
						cmd : 'taobao.promotionmisc.activity.range.add',
						param : params,
						method:"post",
						success : function(rsp, cmd, param) {
							console.log(rsp,param);
						},
						error : function(msg, cmd, param) {
							console.log(msg,param);
						}
					} );
			}else{
				dispatch({
					type: CREAT_ZHEKOU_START
					});
				ajax({
					  api: 'addItemsToMjsActive',
					  method: 'POST',
					  data:params
					},(json) => {
						ajax({
							  api: 'saveMjsItemsToDatabase',
							  method: 'POST',
							  data:{...params,join_items:joinItems}
							},(json_db) => {
								dispatch({
									type: CREAT_ZHEKOU_DONE,
									id:json_db.id,
									activityId,
									message:"已成功创建折扣！",
									actionDone:true
									});
							},(json)=>{
								//(json.code==53) W2超时
								  dispatch({
									type: CREAT_ZHEKOU_ERR,
									message:json.msg+(json.sub_msg?json.sub_msg:""),
									loginFalse:false
									});
							}	
						);
					},(json)=>{
						//(json.code==53) W2超时
						  dispatch({
							type: CREAT_ZHEKOU_ERR,
							message:json.msg+(json.sub_msg?json.sub_msg:""),
							loginFalse:false
							});
					}	
				);
			}
	}
}

export function createMjs(p){
	let exclude_area_id = [];
	let exclude_area_name = [];
	if(p.exclude_area){
		p.exclude_area.map(area=>{
			exclude_area_id.push(area.id);
			exclude_area_name.push(area.name);
		});
	}
	const params={
			db_id:p.id,
			activity_id:p.activity_id,
			name:p.name,
			type:1,
			participate_range:p.participate_range,
			start_time:(typeof p.start_time=="object")?(p.start_time.getFullYear()+"-"+(p.start_time.getMonth()>=1&&p.start_time.getMonth()<=9?0:"")+(p.start_time.getMonth()+1)+"-"+(p.start_time.getDate()<=9?"0":"")+p.start_time.getDate()+" "+(p.start_time.getHours()<=9?"0":"")+p.start_time.getHours()+":"+(p.start_time.getMinutes()<=9?"0":"")+p.start_time.getMinutes()+":"+(p.start_time.getSeconds()<=9?"0":"")+p.start_time.getSeconds() ):p.start_time,
			end_time:(typeof p.end_time=="object")?(p.end_time.getFullYear()+"-"+(p.end_time.getMonth()>=1&&p.end_time.getMonth()<=9?0:"")+(p.end_time.getMonth()+1)+"-"+(p.end_time.getDate()<=9?"0":"")+p.end_time.getDate()+" "+(p.end_time.getHours()<=9?"0":"")+p.end_time.getHours()+":"+(p.end_time.getMinutes()<=9?"0":"")+p.end_time.getMinutes()+":"+(p.end_time.getSeconds()<=9?"0":"")+p.end_time.getSeconds() ):p.end_time,
			is_amount_over:p.is_amount_over,
			total_price:p.total_price?p.total_price*100:"0",
			is_amount_multiple:p.is_amount_multiple,
			is_item_count_over:p.is_item_count_over,
			item_count:p.item_count,
			is_item_multiple:p.is_item_multiple,
			is_shop_member:p.is_shop_member,
			shop_member_level:p.shop_member_level?p.shop_member_level:0,
			is_user_tag:'false',
			user_tag:'',
			is_decrease_money:p.is_decrease_money,
			decrease_amount:p.decrease_amount?p.decrease_amount*100:"0",
			is_discount:p.is_discount,
			discount_rate:p.discount_rate?p.discount_rate*100:"0",
			is_send_gift:p.is_send_gift,
			gift_name:p.gift?p.gift[0].title:"",
			gift_id:p.gift?p.gift[0].num_iid:"0",
			gift_url:p.gift?"http://item.daily.taobao.net/item.htm?id="+p.gift[0].num_iid:"",
			gift_pic_url:p.gift?p.gift[0].pic_url:"",
			is_free_post:p.is_free_post,
			exclude_area:exclude_area_id.length>0?(exclude_area_id.join("*")+"#"+exclude_area_name.join("*")):""
		}

	return (dispatch) => {
	    //发送请求前
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			  window.QN.top.invoke( {
					cmd : 'taobao.promotionmisc.mjs.activity.add',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			ajax({
				  api: "createMjs",
				  method: 'POST',
				  data:params
				},(json) => {
					ajax({
						  api: 'saveMjsToDatabase',
						  method: 'POST',
						  data:{...params,...json}
						},(json_db) => {
							dispatch({
								type: CREAT_ZHEKOU_DONE,
								activityId:json.activity_id,
								id:json_db.id,
								actionDone:false
								});
						},(json)=>{
							//(json.code==53) W2超时
							  dispatch({
								type: CREAT_ZHEKOU_ERR,
								message:json.msg+(json.sub_msg?json.sub_msg:""),
								loginFalse:false
								});
						}	
					);
				},(json)=>{
					//(json.code==53) W2超时
					  dispatch({
						type: CREAT_ZHEKOU_ERR,
						message:json.msg+(json.sub_msg?json.sub_msg:""),
						loginFalse:false
						});
				}	
			);
		}
	}
}

export function getAreas() {
	return (dispatch) => {
	    //发送请求前

	    ajax({
			  api: 'getAreas',
			  method: 'GET',
			  data:{}
			},(json) => {
			  //成功后
				dispatch({
					type: OPEN_AREA_DIALOG,
					area:json.areas.area,
				});
			},(json) => {
				  //失败后
				  //(json.code==53) W2超时
				  dispatch({
					type: CREAT_ZHEKOU_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:false
					});
			});
	}
}


